package com.hooya.fa.eu.biz.dao.manager.special;

import com.hooya.fa.eu.api.bo.JcInfoBO;
import com.hooya.fa.eu.api.bo.PlatformZyInfoBO;
import com.hooya.fa.eu.api.bo.report.ProfitCheckBO;
import com.hooya.integrated.common.core.util.BeanCopyUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2025/3/24
 * @Version 1.1.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class ProfitCheckInitManager {

    /**
     * 构造进仓数据
     *
     * @param info    信息
     * @param belongs 货权
     * @param htrq    合同日期
     * @param hthm    合同号码
     * @param sl      数量
     * @param date    日期
     * @return ProfitCheckBO
     */
    public ProfitCheckBO genJcProfitCheckBO(JcInfoBO info, String belongs, String htrq, String hthm, Integer sl, String date) {
        return BeanCopyUtils.copyPropertiesNew(info, ProfitCheckBO::new, (s, t) -> {
            t.setBelongs(belongs);
            t.setInvoiceDate(htrq);
            t.setInboundDate(date);
            t.setInvoiceNumber(hthm);
            t.setRemainingNum(sl);
            t.setType("采购");
        });
    }

    /**
     * 构造转运数据
     *
     * @param info    信息
     * @param date    日期
     * @param htrq    合同日期
     * @param hthm    合同号码
     * @param place   仓库
     * @param belongs 货权
     * @param sl      数量
     * @return ProfitCheckBO
     */
    public ProfitCheckBO genZyProfitCheckBO(JcInfoBO info, String date, String htrq, String hthm, String place,
                                            String belongs, Integer sl) {
        return BeanCopyUtils.copyPropertiesNew(info, ProfitCheckBO::new, (s, t) -> {
            t.setInboundDate(htrq);
            t.setInvoiceDate(date);
            t.setInvoiceNumber(hthm);
            t.setPlace(place);
            t.setBelongs(belongs);
            t.setRemainingNum(sl);
            t.setType("买");
        });
    }

    /**
     * 构造盘盈数据
     *
     * @param info         信息
     * @param htrq         合同日期
     * @param currencyRate 货币汇率
     * @param pln          pln
     * @param inPriceEur   欧元进价
     * @param inPriceUsd   美元进价
     * @param sl           数量
     * @return ProfitCheckBO
     */
    public ProfitCheckBO genPyProfitCheckBO(JcInfoBO info, String htrq, Double currencyRate, Double pln,
                                            Double inPriceEur, Double inPriceUsd, Integer sl) {
        return BeanCopyUtils.copyPropertiesNew(info, ProfitCheckBO::new, (s, t) -> {
            t.setInboundDate(htrq);
            t.setRemainingNum(sl);
            t.setCurrencyRate(currencyRate);
            t.setPln(pln);
            if (Objects.isNull(inPriceUsd)) {
                t.setInPriceEur(inPriceEur);
            } else {
                t.setInPriceUsd(inPriceUsd);
            }
            t.setType("盘盈");
        });
    }

    /**
     * 多次转运查找所需数据
     *
     * @param allZyList  所有转运数据
     * @param allJcList  所有进仓数据
     * @param jcInfoList 进仓信息
     * @param sku        sku
     * @param htrq       合同日期
     * @param hthm       合同号码
     * @param place      仓库
     * @param belongs    货权
     * @return list
     */
    public List<JcInfoBO> getFilterList(List<PlatformZyInfoBO> allZyList, List<JcInfoBO> allJcList,
                                        List<JcInfoBO> jcInfoList, String sku, String htrq, String hthm,
                                        String place, String belongs) {
        List<JcInfoBO> res = new LinkedList<>();
        //N次转运
        List<PlatformZyInfoBO> zyList = allZyList.stream()
                .filter(l -> l.getHtrq().compareTo(htrq) <= 0)
                .filter(l -> l.getSku().equals(sku) && l.getZySl() != 0 &&
                        !l.getHthm().equals(hthm) && l.getPlace().equals(place) &&
                        (l.getTobelongs().equals(belongs) || l.getTobelongs().equals("GLOBALWAY")))
                .sorted(Comparator.comparing(PlatformZyInfoBO::getHtrq).reversed())
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(zyList) && belongs.equals("COSTWAY-DE")) {
            zyList = allZyList.stream()
                    .filter(l -> l.getHtrq().compareTo(htrq) <= 0)
                    .filter(l -> l.getSku().equals(sku) && l.getZySl() != 0 &&
                            !l.getHthm().equals(hthm) && l.getPlace().equals(place) &&
                            l.getTobelongs().startsWith("COSTWAY"))
                    .sorted(Comparator.comparing(PlatformZyInfoBO::getHtrq).reversed())
                    .collect(Collectors.toList());
        }
        if (!CollectionUtils.isEmpty(zyList)) {
            String zyHtrq = zyList.stream().map(PlatformZyInfoBO::getHtrq).min(Comparator.comparing(s -> s)).get();
            for (String t : zyList.stream().map(l -> l.getZcck() + "_" + l.getFrombelongs())
                    .distinct().collect(Collectors.toList())) {
                String zyPlace = t.split("_")[0];
                String zyBelongs = t.split("_")[1];
                List<JcInfoBO> list = allJcList.stream().filter(l -> l.getSku().equals(sku) && l.getPlace().equals(zyPlace) &&
                                (l.getBelongs().equals(zyBelongs) || l.getBelongs().equals("GLOBALWAY")))
                        .collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(list)) {
                    for (JcInfoBO jc : list) {
                        if (jcInfoList.stream().noneMatch(l -> l.getHthm().equals(jc.getHthm()) &&
                                l.getSku().equals(jc.getSku()) && l.getPlace().equals(jc.getPlace()))) {
                            jcInfoList.add(jc);
                        }
                        res.add(jcInfoList.stream()
                                .filter(l -> l.getHthm().equals(jc.getHthm()) &&
                                        l.getSku().equals(jc.getSku()) && l.getPlace().equals(jc.getPlace()))
                                .findFirst().get());
                    }
                } else {
                    res.addAll(getFilterList(allZyList, allJcList, jcInfoList,
                            sku, zyHtrq, hthm, zyPlace, zyBelongs));
                }
            }
        }
        return res;
    }

}
